package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.ExportFormDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartPenetrateResultDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultCallStatisticsDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultDTO;
import com.xbongbong.pro.statistic.service.ChartExportService;
import com.xbongbong.pro.statistic.through.pojo.dto.ResultThroughDTO;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhouwq
 * @date 2019/6/10 9:01
 */

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart")
public class ChartExcelController {

    @Resource
    private ChartExportService chartExportService;

    /**
     * 导出穿透数据
     * @param resultThroughDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2019/5/29 19:32
     */
    @RequestMapping(value = "/export/through/data")
    public String exportChartThroughData(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br){
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = chartExportService.exportChartThroughData(resultThroughDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }

    /**
     * 导出数据
     * @param chartResultDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2019/5/29 19:32
     */
    @RequestMapping(value = "/export/data")
    public String exportChartData(@RequestBody ChartPenetrateResultDTO chartResultDTO, BindingResult br){
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = chartExportService.exportChartData(chartResultDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }

    /**
     * 导出pk榜数据
     * @param chartResultDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2019/5/29 19:32
     */
    @RequestMapping(value = "/export/data/pk")
    public String exportPkChartData(@RequestBody ChartResultDTO chartResultDTO, BindingResult br){
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = chartExportService.exportPkChartData(chartResultDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }

    /**
     * 导出目标完成情况
     *
     * @param chartResultDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/6/20 21:35
     * @since v1.0
     */
    @RequestMapping(value = "/export/data/performance")
    public String exportPerformance(@RequestBody ChartResultDTO chartResultDTO, BindingResult br) {
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = chartExportService.exportPerformance(chartResultDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }

    /**
     * 导出呼叫统计
     *
     * @param chartResultDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author youli.chen
     * @date 2019/6/20 21:35
     * @since v1.0
     */
    @RequestMapping(value = "/export/data/call/statistics")
    public String exportCallStatistics(@RequestBody ChartResultCallStatisticsDTO chartResultDTO, BindingResult br) {
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = chartExportService.exportCallStatistics(chartResultDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }
}
